import cv2
import numpy as np
from flet_core.alignment import center


# 读取图像
# image = cv2.imread('1.png')
# cv2.imshow("1", image)
#
# 转换颜色空间
# 转为灰度图像
# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2.imshow('Gray image', gray_image)
#
# Canny边缘检测
# edges = cv2.Canny(gray_image, 100, 200)
# cv2.imshow('Edges',edges)
# GaussianBlur高斯模糊
# blurred_image = cv2.GaussianBlur(edges, (5,5),0)
# cv2.imshow('Blurred Image', blurred_image)
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()


############################

# def get_center(img_canny,):
#     y_top = np.nonzero([max(row) for row in img_canny[400:]])[0][0] + 400
#     x_top = int(np.mean(np.nonzero(edges[y_top])))
#
#     y_bottom = y_top + 50
#     for row in range(y_bottom, H):
#         if edges[row, x_top] != 0:
#             y_bottom = row
#             break
#
#     x_center, y_center = x_top, (y_top + y_bottom) // 2
#     return img_canny, x_center,y_center

def get_center():
    image = cv2.imread('1_2.png')
    blurred_img = cv2.GaussianBlur(image, (5,5),0)#高斯模糊
    canny_img = cv2.Canny(blurred_img, 1, 10)#边缘检测

    height, width = canny_img.shape
    crop_img = canny_img[300:int(height/2), 0:width]
    # #消除小跳棋
    # for y in range(max_loc[1], max_loc[1]+150):
    #     for x in range(max_loc[0], max_loc[0] + 50):
    #         canny_img[y][x] = 0

    # 获取中心点坐标
    crop_h , crop_w = crop_img.shape
    center_x, center_y = 0, 0
    max_x = 0
    for y in range(crop_h):
        for x in range(crop_w):
            if crop_img[y,x] == 255:
                if center_x == 0:
                    center_x = x
                if x > max_x:
                    center_y = y
                    max_x = x
    cv2.circle(crop_img, (center_x, center_y), 10, 255, -1)
    # cv2.circle(image, (center_x , center_y+ 300), 10, 255, -1)
    # cv2.imshow('img2',image)
    cv2.imshow('Img',crop_img)

def main():
    # get_center()
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    image = cv2.imread('1.png')
    # 水平翻转
    flipped_image = cv2.flip(image, 1)
    cv2.imshow('Flipped Image', flipped_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()